package com.example.rdcproject.ving.notes.view.Admin;

import com.example.rdcproject.ving.notes.service.ConnectionPool;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

public class BlackView extends JFrame  {
    private JFrame frame;
    private JTable userTable;
    private DefaultTableModel tableModel;

    private JButton blockButton;
    private JButton unblockButton;

    private int selectedRow;

    private Connection connection;
    private Statement statement;
    private ResultSet resultSet;

    public BlackView(){
        initialize();
        showAllUsers();
    }

    ConnectionPool connectionPool = null;


    private void initialize() {
        frame = new JFrame("管理员页面");
        frame.setBounds(100, 100, 500, 400);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().setLayout(new BorderLayout());

        JPanel titlePanel = new JPanel(new FlowLayout());
        JLabel titleLabel = new JLabel("黑名单");
        titlePanel.add(titleLabel);

        JPanel buttonPanel = new JPanel(new FlowLayout());
        blockButton = new JButton("拉黑");
        blockButton.setEnabled(false);
        unblockButton = new JButton("取消拉黑");
        unblockButton.setEnabled(false);
        buttonPanel.add(blockButton);
        buttonPanel.add(unblockButton);

        tableModel = new DefaultTableModel(new Object[][] {}, new String[] {"用户名", "是否被拉黑" });
        userTable = new JTable(tableModel);
        userTable.getSelectionModel().addListSelectionListener(e -> {
            if (userTable.getSelectedRowCount() > 0) {
                blockButton.setEnabled(true);
                unblockButton.setEnabled(true);
                selectedRow = userTable.getSelectedRow();
            } else {
                blockButton.setEnabled(false);
                unblockButton.setEnabled(false);
                selectedRow = -1;
            }
        });

        JScrollPane scrollPane = new JScrollPane(userTable);

        frame.getContentPane().add(titlePanel, BorderLayout.NORTH);
        frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
        frame.getContentPane().add(buttonPanel, BorderLayout.SOUTH);

        blockButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {

                    connectionPool = ConnectionPool.getInstance();
                    connection = connectionPool.getConnection();
                    statement = connection.createStatement();

                    String nickname = (String) userTable.getValueAt(selectedRow, 0);
                    statement.executeUpdate("UPDATE user SET user_isBlack=1 WHERE user_nickName =" + "'nickname'");
                    userTable.setValueAt("true", selectedRow, 1);
                    blockButton.setEnabled(false);
                    unblockButton.setEnabled(false);
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        });

        unblockButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {

                    connectionPool = ConnectionPool.getInstance();
                    connection = connectionPool.getConnection();
                    statement = connection.createStatement();

                    statement.executeUpdate("UPDATE users SET user_isBlack=0 WHERE user_nickname=" + userTable.getValueAt(selectedRow, 0));
                    userTable.setValueAt("false", selectedRow, 1);
                    blockButton.setEnabled(false);
                    unblockButton.setEnabled(false);
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        });

        frame.setVisible(true);
    }


    public void showAllUsers() {
        ConnectionPool connectionPool = null;
        try {
            // 连接到数据库
            connectionPool = ConnectionPool.getInstance();
            connection = connectionPool.getConnection();
            statement = connection.createStatement();
            // 查询所有用户
            String query = "SELECT user_nickname,user_isBlack FROM user";
            Statement statement = connection.createStatement();
           resultSet = statement.executeQuery(query);
            // 将结果集转化为 TableModel 并展示在表格中
            DefaultTableModel model = new DefaultTableModel();
            ResultSetMetaData rsmd = resultSet.getMetaData();

            int columnCount = rsmd.getColumnCount();
            for (int i = 1; i <= columnCount; i++) {
                model.addColumn(rsmd.getColumnName(i));
            }
            while (resultSet.next()) {
                Object[] row = new Object[columnCount];
                for (int i = 1; i <= columnCount; i++) {
                    row[i - 1] = resultSet.getObject(i);
                }
                model.addRow(row);
            }

            // 设置JTable的数据模型
            userTable.setModel(model);
            // 关闭连接
            statement.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            connectionPool.releaseConnection(connection);
        }
    }


}
